package com.polaris.live.viewmodel.listeners

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.net.homeService
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.go_resp.UserIdsBean

/**
 * OnStreamLiveDataListener
 *
 * @author Created by lucas on 2023/11/28 14:25
 */
interface OnStreamLiveDataListener {

    /**
     * 监听
     */
    val userLiveLiveData: MutableLiveData<DisplayUserBean>

    /**
     * 获取用户的直播信息
     */
    fun getUserLiveStream(viewModel: BaseViewModel, otherId: Long) {
        viewModel.request({ homeService.getDisplayUsers(UserIdsBean(otherId)) }, {
            it.firstOrNull()?.apply {
                userLiveLiveData.value = this
            }
        })
    }
}

/**
 * 获取用户的直播信息
 */
fun <VM> VM.getUserLiveStream(otherId: Long) where VM : OnStreamLiveDataListener, VM : BaseViewModel {
    getUserLiveStream(this, otherId)
}